/**
 * 这个是直接在百度上深度搜索的关于第一题的AI解答
 * 这里总体设计思路和我的代码差不多，都是累计分钟数然后计算差值
 * 不一样的是我将所有UTC时间加8转换称CTS时间，可以避免计算负数跨天的问题
 *
 */

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeDifferenceCalculator {
    private static final Pattern TIME_PATTERN = Pattern.compile("(\\d{2}):(\\d{2})(\\w+)");
    private static final int CST_UTC_OFFSET = 480; // CST比UTC快480分钟（8小时）

    public static int findMinDifference(List<String> timePoints) {
        int[] minutes = new int[timePoints.size()];

        // 1. 转换所有时间为UTC分钟数
        for (int i = 0; i < timePoints.size(); i++) {
            minutes[i] = parseAndConvertToMinutes(timePoints.get(i));
        }

        // 2. 排序以便相邻比较
        Arrays.sort(minutes);

        // 3. 计算最小差值（考虑跨天情况）
        int minDiff = Integer.MAX_VALUE;
        for (int i = 1; i < minutes.length; i++) {
            minDiff = Math.min(minDiff, minutes[i] - minutes[i - 1]);
        }
        minDiff = Math.min(minDiff, 1440 + minutes[0] - minutes[minutes.length - 1]); // 跨天情况

        return minDiff;
    }

    private static int parseAndConvertToMinutes(String timeStr) {
        Matcher matcher = TIME_PATTERN.matcher(timeStr);
        if (!matcher.find()) throw new IllegalArgumentException("Invalid time format");

        int hour = Integer.parseInt(matcher.group(1));
        int minute = Integer.parseInt(matcher.group(2));
        String zone = matcher.group(3);

        // 统一转换为UTC分钟数
        int totalMinutes = hour * 60 + minute;
        if ("CST".equals(zone)) {
            totalMinutes -= CST_UTC_OFFSET; // CST转UTC需减8小时
        }
        return (totalMinutes + 1440) % 1440; // 处理负数（跨天）
    }

    public static void main(String[] args) {
        System.out.println(findMinDifference(Arrays.asList("08:01CST", "00:00UTC"))); // 示例1 → 1
        System.out.println(findMinDifference(Arrays.asList("00:03CST", "08:00CST", "00:00UTC"))); // 示例2 → 0
    }
}
